package net.javastone.toolkit.robot.dingding.resource;

import net.javastone.toolkit.robot.dingding.model.DingDingRequest;
import net.javastone.toolkit.robot.dingding.model.DingDingResponse;
import net.javastone.toolkit.robot.dingding.model.GitlabResponse;
import net.javastone.toolkit.robot.dingding.model.GitlabWikiRequest;

/**
 * Created by sdliang on 2018/11/6.
 */
public class GitLabWebHookResource {


  protected static final String WIKI_TITLE = "[WIKI更新] 项目: %s, 标题: %s";
  protected static final String WIKI_TEXT = "%s 更新了WIKI, 更新内容: %s";

  /**
   * gitlab的wiki请求转成钉钉的请求
   *
   * @param wiki
   * @return
   */
  protected DingDingRequest of(GitlabWikiRequest wiki) {
    DingDingRequest request = new DingDingRequest();
    request.setMsgtype(DingDingRequest.MsgType.LINK.toString().toLowerCase());
    String title = String.format(WIKI_TITLE,
        wiki.getProject().getDescription(),
        wiki.getObject_attributes().getTitle());
    String text = String
        .format(WIKI_TEXT, wiki.getUser().getName(), wiki.getObject_attributes().getMessage());
    request
        .setLink(new DingDingRequest.Link(title, text, "", wiki.getObject_attributes().getUrl()));
    return request;
  }

  /**
   * 钉钉的响应请求转成gitable的响应
   *
   * @param response
   * @return
   */
  protected GitlabResponse to(DingDingResponse response) {
    GitlabResponse gitlab = new GitlabResponse();
    gitlab.setErrcode(response.getErrcode());
    gitlab.setErrmsg(response.getErrmsg());
    return gitlab;
  }
}
